package com.example.petplanet.controller;


import com.example.petplanet.domain.Content;
import com.example.petplanet.dto.CallBackDTO;
import com.example.petplanet.result.Result;
import com.example.petplanet.service.ContentService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/content")
public class ContentController {

    private final ContentService contentService;

    public ContentController(ContentService contentService) {
        this.contentService = contentService;
    }

    @PostMapping("/callBack")
    public Result<Object> CallBack(@RequestBody CallBackDTO callBackDTO) {
        System.out.println(callBackDTO);
        return contentService.CallBack(callBackDTO);
    }

    @GetMapping("/get/{key}")
    public Result<Object> get(@PathVariable String key){
        return Result.success(ContentService.callbackMap.get(key));
    }

    @GetMapping("/getAllContent")
    public Result<Object> getAllContent(){
        return contentService.getAllContent();
    }

    @GetMapping("/getMyContents")
    public Result<Object> getMyContents(){
        return contentService.getMyContents();
    }

    @GetMapping("/getContentById")
    public Result<Object> getContentById(@RequestParam Long contentId){
        return contentService.getContentById(contentId);
    }

    @PostMapping("/editContent")
    public Result<Object> editContent(@RequestParam Long contentId,
                                      @RequestParam String description,
                                      @RequestParam boolean permission){
        return contentService.editContent(contentId,description,permission);
    }

    //暂时无用
    @DeleteMapping("deleteFileByObjectName")
    public Result<Object> deleteFileByObjectName(@RequestParam String objectName){
        return contentService.deleteFileByObjectName(objectName);
    }


    @DeleteMapping("/deleteContent")
    public Result<Object> deleteContent(@RequestParam Long contentId){
        return contentService.deleteContent(contentId);
    }

    @PostMapping("/launchContent")
    public Result<Object> launchContent(@RequestParam String title,
                                        @RequestParam String description,
                                        @RequestParam boolean permission,
                                        @RequestParam String uploadId,
                                        @RequestParam int total){

        Content content=new Content();
        content.setTitle(title);
        content.setDescription(description);
        content.setPermission(permission);
        return contentService.launchContent(content,uploadId,total);
    }


    @PostMapping("/markBook")
    public Result<Object> markBook(@RequestParam Long contentId
    ){
        return contentService.markBook(contentId);
    }


    @DeleteMapping("/cancelMarkBook")
    public Result<Object> cancelMarkBook(@RequestParam Long contentId){
        return contentService.cancelMarkBook(contentId);
    }


    @PostMapping("/likeContent")
    public Result<Object> likeContent(@RequestParam Long contentId){
        return contentService.likeContent(contentId);
    }

    @DeleteMapping("/cancelLikeContent")
    public Result<Object> cancelLikeContent(@RequestParam Long contentId){
        return contentService.cancelLikeContent(contentId);
    }

    @GetMapping("/getRecommendContents")
    public Result<Object> getRecommendContents(){
        try {
            return contentService.getRecommendContents();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return Result.error();
        }
    }




}
